/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cashmate.struts;

import com.cashmate.hibernate.dao.DccrDAO;
import com.cashmate.hibernate.pojo.Dccr;
import com.cashmate.hibernate.pojo.Employees;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.hibernate.Criteria;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author dean
 */
@Results({
    @Result(name = "successweek", type = "redirect", location = "/dccr.jsp", params = {"date", "${date}", "branch", "${branch}"}),
    @Result(name = "successmonth", type = "redirect", location = "/monthperformance.jsp", params = {"month", "${month}", "year", "${year}", "branch", "${branch}"})
})
public class ChoosePerf extends ActionSupport {

    private final Employees emps = (Employees) ServletActionContext.getRequest().getSession().getAttribute("employee");
    private final Logger logger = Logger.getLogger(ChoosePerf.class);
    private final DccrDAO dccrdao = (DccrDAO) ServletActionContext.getServletContext().getAttribute("dccrDAO");
    private String cutoffDate;
    private String type, date, branch, month, year;

    @Action(value = "choose-perf")
    @Override
    public String execute() throws Exception {
        try {
            if (cutoffDate.equals("-1")) {
                return NONE;
            }
            String[] parsed = cutoffDate.split("-");
            DetachedCriteria criteria = DetachedCriteria.forClass(Dccr.class);

            if (type.equals("week")) {
                branch = parsed[0];
                int branchid = Integer.parseInt(parsed[0]);
                date = parsed[1];
                criteria.add(Restrictions.eq("dategenerated", date));
                criteria.add(Restrictions.eq("branchid", branchid));
                criteria.add(Restrictions.eq("isactive", true));

                int count = dccrdao.countByCriteria(criteria);
                criteria.setProjection(null);
                criteria.setResultTransformer(Criteria.ROOT_ENTITY);

                if (count > 0) {
                    return "successweek";
                }

            } else if (type.equals("month")) {
                month = parsed[0];
                year = parsed[1];
                branch = parsed[2];
                int branchid = Integer.parseInt(parsed[2]);
                System.out.println(month+" "+year+" "+branch);
                criteria.add(Restrictions.eq("branchid", branchid));
                criteria.add(Restrictions.like("dategenerated", month + "/%/" + year));
                criteria.add(Restrictions.eq("isactive", true));

                int count = dccrdao.countByCriteria(criteria);
                criteria.setProjection(null);
                criteria.setResultTransformer(Criteria.ROOT_ENTITY);
                System.out.println(count);
                if (count > 0) {
                    return "successmonth";
                }
            }
        } catch (Exception e) {
            if (emps != null) {
                logger.fatal("(" + emps.getIdnumber() + "):" + e.getLocalizedMessage(), e);
            } else {
                logger.fatal(e.getLocalizedMessage(), e);
            }
            e.printStackTrace();
            //add action errors this will correspond to <s:actionerror/> in a struts-enabled jsp page.
            addActionError(e.getLocalizedMessage());
            throw e;
        } finally {
            DccrDAO.close();
        }
        return NONE;
    }

    public String getBranch() {
        return branch;
    }

    public void setCutoffDate(String cutoffDate) {
        this.cutoffDate = cutoffDate;
    }

    public String getDate() {
        return date;
    }

    public String getMonth() {
        return month;
    }

    public void setMonth(String month) {
        this.month = month;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getYear() {
        return year;
    }
}
